#include "consoledockwin.h"
#include <qDebug>
#include <QMessageBox>

QByteArray HexStringToByteArray(QString HexString)
{
	bool ok;
	QByteArray ret;
	HexString = HexString.trimmed();
	HexString = HexString.simplified();
	QStringList sl = HexString.split(" ");

	foreach(QString s, sl)
	{
		if (!s.isEmpty())
		{
			char c = s.toInt(&ok, 16) & 0xFF;
			if (ok)
			{
				ret.append(c);
			}
			else
			{
				//
			}
		}
	}
	return ret;
}

// preset text impletmention
PreSetText::PreSetText(QString text)
{
	m_obj_name = "console_dock_win" + text;
	setObjectName(m_obj_name);
	m_check.setCheckState(Qt::Unchecked);
	m_hex_text.setText("HEX");
	m_send_button.setText(text);
	m_line.setAlignment(Qt::AlignLeft);
	m_line.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	m_hLayout = new QHBoxLayout();
	m_hLayout->addWidget(&m_check, 0);
	m_hLayout->addWidget(&m_hex_text, 0);
	m_hLayout->addWidget(&m_send_button, 0);
	m_hLayout->addWidget(&m_line, 1);
	connect(&m_send_button, &QPushButton::clicked, this, &PreSetText::on_send_clicked);

	QSettings settings;
	m_line.setText(settings.value(m_obj_name).toString());
	m_check.setCheckState(settings.value(m_obj_name + "check").toBool() ? Qt::Checked : Qt::Unchecked);
}

PreSetText::~PreSetText(void)
{
	QSettings settings;
	settings.setValue(m_obj_name, m_line.text());
	settings.setValue(m_obj_name + "check", m_check.checkState() == Qt::Checked);
}

QHBoxLayout *PreSetText::getLayou(void)
{
	return m_hLayout;
}

void PreSetText::on_send_clicked(void)
{
	QByteArray data;
	if (m_check.checkState() == Qt::Checked)
	{
		data = HexStringToByteArray(m_line.text());
	}
	else
	{
		data = QByteArray::fromStdString(m_line.text().toStdString());
	}
	emit send(data);
}

ConsoleDockWin::ConsoleDockWin(QWidget *parent)
	:QDockWidget("console", parent)
{
	setObjectName("console_dock_win");
	setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
	setAllowedAreas(Qt::AllDockWidgetAreas);
	
	m_log = new QTextBrowser();
	m_hex_check = new QCheckBox();
	m_hex_check->setCheckState(Qt::Unchecked);
	m_hex_text.setText("HEX");
	m_send_line = new QLineEdit();
	m_send_button = new QPushButton(tr("Send"));
	m_preset_text_scrollarea = new QScrollArea();
	m_main_splitter = new QSplitter();
	QVBoxLayout *leftVlayout = new QVBoxLayout();
	QHBoxLayout *leftBottomHlayout = new QHBoxLayout();
	
	leftBottomHlayout->addWidget(m_send_line);
	leftBottomHlayout->addWidget(m_send_button); 
	leftBottomHlayout->addWidget(&m_hex_text);
	leftBottomHlayout->addWidget(m_hex_check);
	leftVlayout->addWidget(m_log);
	leftVlayout->addLayout(leftBottomHlayout);
	
	QFrame *leftMainFrame = new QFrame();
	leftMainFrame->setLayout(leftVlayout);
	m_main_splitter->addWidget(leftMainFrame);

	QVBoxLayout *textListLayout = new QVBoxLayout();
	m_preset_text_list.clear();
	for (int i = 0; i < MAX_PRESET_TEXT_CNT; i++)
	{
		PreSetText *presetText = new PreSetText(QString::number(i));
		m_preset_text_list.append(presetText);
		textListLayout->addLayout(presetText->getLayou());
		connect(presetText, &PreSetText::send, this, &ConsoleDockWin::on_recv_send_data);
	}
	textListLayout->addStretch();
	QFrame *textListFrame = new QFrame(m_preset_text_scrollarea);
	textListFrame->setLayout(textListLayout);
	m_preset_text_scrollarea->setWidget(textListFrame);
	m_preset_text_scrollarea->setWidgetResizable(true);
	m_main_splitter->addWidget(m_preset_text_scrollarea);

	QSettings settings;
	m_main_splitter->restoreState(settings.value("consoleSpliter").toByteArray());
	m_send_line->setText(settings.value("console_send_line").toString());
	m_hex_check->setCheckState(settings.value("console_hex_check").toBool() ? Qt::Checked : Qt::Unchecked);

	connect(m_send_button, &QPushButton::clicked, this, &ConsoleDockWin::on_send_clicked);

	setWidget(m_main_splitter);
}

ConsoleDockWin::~ConsoleDockWin(void)
{
	QSettings settings;
	for (auto text : m_preset_text_list)
	{
		delete text;
	}
	m_preset_text_list.clear();
	settings.setValue("consoleSpliter", m_main_splitter->saveState());
	settings.setValue("console_send_line", m_send_line->text());
	settings.setValue("console_hex_check", m_hex_check->checkState() == Qt::Checked);
}

void ConsoleDockWin::on_recv_send_data(QByteArray data)
{
	qDebug() << data;
	emit send(data);
}

void ConsoleDockWin::on_send_clicked(void)
{
	QByteArray data;
	if (m_hex_check->checkState() == Qt::Checked)
	{
		data = HexStringToByteArray(m_send_line->text());
	}
	else
	{
		data = QByteArray::fromStdString(m_send_line->text().toStdString());
	}
	qDebug() << data;
	emit send(data);
}

void ConsoleDockWin::log_data(QString log)
{
	/*
	m_log->setText(m_log->toPlainText() + log);
	m_log->moveCursor(QTextCursor::End);
	*/
}

void ConsoleDockWin::log_data(QByteArray log)
{
	// m_log->setText(m_log->toPlainText() + QString::fromStdString(log.toStdString()));
	m_log->insertPlainText(QString::fromStdString(log.toStdString()));
	m_log->moveCursor(QTextCursor::End);
}